package pattern.command;

import pattern.active.SadActiveObject;

/**
 * 悲伤命令模式
 *
 * @author sun.ye
 * @since 2020-07-14 18:55
 */
public class SadCommandImpl implements SadCommand,SuicideCommand{

    private SuicideCommand suicideCommand;
    private SadActiveObject engine;
    private long startTime;
    private long maxTime;
    private boolean crying = false;

    public SadCommandImpl(SuicideCommand suicideCommand, SadActiveObject engine, long maxTime) {
        this.suicideCommand = suicideCommand;
        this.engine = engine;
        this.maxTime = maxTime;
    }

    @Override
    public void cry() {
        long currentTime = System.currentTimeMillis();
        if (!crying) {
            crying = true;
            startTime = currentTime;
            engine.addCommand(this);
        }else if (currentTime - startTime < maxTime) {
            engine.addCommand(this);
        }else {
            engine.addCommand(suicideCommand);
        }
    }

    @Override
    public void suicide() {

    }
}